package com.slw.emergency.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.slw.emergency.entity.EmergencyDetail;
import com.slw.emergency.enums.StatusEnum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class AnalysisUtil {
    public static List<EmergencyDetail> getList(String str){
        List<EmergencyDetail> emergencyDetailList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(str);
        Map<String,Object> map = (Map<String,Object>)jsonObject;//    //json对象转Map
        Map<String,Object> map1 = (Map<String,Object>)map.get("community");
        map1.forEach((k,v)->{
            Map<String,Object> map2 =  (Map<String,Object>)v;
            map2.forEach((k1,v1)->{
                Map<String,Object> map3 =  (Map<String,Object>)v1;
                map3.forEach((k2,v2)->{
                    JSONArray jsonArray =  (JSONArray)v2;
                    for (int i = 0 ; i<jsonArray.size();i++){
                        JSONObject json = jsonArray.getJSONObject(i);
                        emergencyDetailList.add(getEmergencyDetail(json));
                    }
                });
            });
        });
        return emergencyDetailList;
    }

    /**
     * 将json转化成对象
     * @param json
     * @return
     */
    private static EmergencyDetail getEmergencyDetail(JSONObject json){
        EmergencyDetail emergencyDetail = new EmergencyDetail();
        emergencyDetail.setProvince(json.getString("province"));
        emergencyDetail.setCity(json.getString("city"));
        emergencyDetail.setDistrict(json.getString("district"));
        emergencyDetail.setCounty(json.getString("county"));
        emergencyDetail.setStreet(json.getString("street"));
        emergencyDetail.setCommunity(json.getString("community"));
        emergencyDetail.setShowAddress(json.getString("show_address"));

        emergencyDetail.setCntIncUncertain(json.getString("cnt_inc_uncertain"));
        emergencyDetail.setCntIncCertain(json.getString("cnt_inc_certain"));
        emergencyDetail.setCntIncDie(json.getString("cnt_inc_die"));
        emergencyDetail.setCntIncRecure(json.getString("cnt_inc_recure"));

        emergencyDetail.setCntSumUncertain(json.getString("cnt_sum_uncertain"));
        emergencyDetail.setCntSumCertain(json.getString("cnt_sum_certain"));
        emergencyDetail.setCntSumDie(json.getString("cnt_sum_die"));
        emergencyDetail.setCntSumRecure(json.getString("cnt_sum_recure"));

        emergencyDetail.setFullAddress(json.getString("full_address"));
        emergencyDetail.setLng(json.getFloat("lng"));
        emergencyDetail.setLat(json.getFloat("lat"));
        emergencyDetail.setSource(json.getJSONArray("source").toJSONString());
        emergencyDetail.setDistance(json.getFloat("distance"));
        emergencyDetail.setCreateTime(new Date());
        emergencyDetail.setWhetherNow(StatusEnum.YES.getStutus());
        return emergencyDetail;
    }

}
